package DAO;

import java.awt.event.*;
import javax.swing.*;

public class AddClass extends JFrame {
	private JPanel contentPane = new JPanel();
	private JLabel lab_ClassNo, lab_StuNum;
	private JTextField tf_ClassNO, tf_StuNum;
	int getNum = 0;

	public AddClass() {
		setSize(340, 200);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_ClassNo = new JLabel("请输入新班级：");
		lab_ClassNo.setBounds(45, 20, 110, 25);
		tf_ClassNO = new JTextField(10);
		tf_ClassNO.setBounds(160, 20, 120, 25);
		contentPane.add(lab_ClassNo);
		contentPane.add(tf_ClassNO);

		lab_StuNum = new JLabel("请输入班级人数：");
		lab_StuNum.setBounds(45, 50, 110, 25);
		tf_StuNum = new JTextField(10);
		tf_StuNum.setBounds(160, 50, 120, 25);
		tf_StuNum.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (Character.isDigit(c)) {
					return;
				} else {
					e.consume();// 清除输入的非数字内容
				}
			}
		});
		tf_StuNum.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					add();
					JOptionPane.showMessageDialog(null, "班级已建立！");
				}
			}
		});
		contentPane.add(lab_StuNum);
		contentPane.add(tf_StuNum);

		JButton btn_Add = new JButton("确定");
		btn_Add.setBounds(70, 100, 60, 25);
		btn_Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add();
				JOptionPane.showMessageDialog(null, "班级已建立！请添加学生，完成之后点击关闭按钮！");
			}
		});
		JButton btn_Exit = new JButton("关闭");
		btn_Exit.setBounds(200, 100, 60, 25);
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		contentPane.add(btn_Add);
		contentPane.add(btn_Exit);

		setVisible(true);
	}
//利用循环结构获取所有新添加的学生信息
	public void add() {
		getNum = Integer.parseInt(tf_StuNum.getText().trim());
		for (int i = 0; i < getNum; i++) {
			new AddStudent();
		}
	}
}
